// cannon.h

#ifndef cannon_h
#define cannon_h

#include <gl/gl.h>
#include <gl/glu.h>

#include "gmath.h"

class Cannon {
public:
    float rotY,             // rotation of cannon about vertical y axis
          rotHoriz;         // rotation of cannon about local horiz axis

    GLUquadricObj* quadric;


    Cannon() {
        rotY = 0.0f;
        rotHoriz = 0.0f;

        quadric = gluNewQuadric();
    }

    ~Cannon() {
        if (quadric != NULL) {
			gluDeleteQuadric(quadric);
            quadric = NULL;
        }
    }

    void Render() {

		// be sure to push and pop the cannon transforms 
		// so that objects drawn later will not be affected by these
		glPushMatrix();

        glRotatef(rotY, 0, 1, 0);
        glRotatef(rotHoriz, 1, 0, 0);

        glColor3f(0, 1, 0);
        gluCylinder(quadric, 0.5, 1.0f, 5, 20, 10);

		glPopMatrix();

    }

	// get the direction cannon is pointed
    Vector3 GetDirection() {
        Vector3 direction;
        direction.set(0, 0, 1);
        Matrix4 mRotY, mRotHoriz, temp;

        mRotY.makeRotationMatrixY(rotY);
        mRotHoriz.makeRotationMatrixX(rotHoriz);

        temp = Multiply(mRotY, mRotHoriz);

        direction = Multiply(temp, direction);


        return direction;
    }

	// get the cannon endpoint to use as initial projectile position
	Vector3 GetEndPoint() {
		// our simple cannon assumes located at origin
		// so endpoint is <0, 0, 0> + length * cannon_direction
		// length set to 5 above in gluCylinder
		
        Vector3 endpoint;

        endpoint = GetDirection();

		endpoint.scale(5);

        return endpoint;
    }

};

#endif